package com.sphd.emanu.activity.materialsstorelist;

import android.content.Context;
import android.support.v7.app.AlertDialog;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.goods.Category;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.L;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author Hu
 * @Time 2017/8/21 -- 18:41.
 * @Email 87683202@qq.com
 */

public class MaterialsStoreListPresenter extends BasePresenterImpl<MaterialsStoreListContract.View> implements MaterialsStoreListContract.Presenter {

    @Override
    public void selectNextCategory(int id) {
        Call<String> call = emanuService.selectNextCategory(id);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                L.e(response.toString());
                Category category = GsonUtil.GsonToBean(response.body().toString(), Category.class);
                mView.setCate(category);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void deleteMaterial(Integer id, final int pos, final SweetAlertDialog sweetAlertDialog) {
        Call<JsonObject> jsonObjectCall = emanuService.deleteMaterial(id);
        jsonObjectCall.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString() + "---" + response.body().toString());
                mView.setRes(res(response), pos, sweetAlertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void addMaterialCategory(Integer level, String name, Integer org, Integer parent, final AlertDialog viewDialog) {
        emanuService.addMaterialCategory(level, name, org, parent).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString() + "////" + response.body().toString());
                mView.setAddRes(res(response), viewDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void deleteMaterialCategory(Integer categoryId, final int pos, final SweetAlertDialog sweetAlertDialog) {
        emanuService.deleteMaterialCategory(categoryId).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString() + "---" + response.body().toString());
                mView.setDelRes(res(response), pos, sweetAlertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void updateMaterialCategory(int id, Integer level, String name, Integer oid, Integer paId, final Integer pos, final AlertDialog viewDialog) {
        emanuService.updateMaterialCategory(id, level, name, oid, paId).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                mView.setAddRes(res(response), viewDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
}
